package com.buybuyyi.stylistcrm.order.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.buybuyyi.stylistcrm.order.dao.PurchaseOrderGoodsDao;
import com.buybuyyi.stylistcrm.order.mapper.PurchaseOrderGoodsMapper;
import com.buybuyyi.stylistcrm.order.po.PurchaseOrderGoods;
import stylistcrm.core.services.order.dto.OrderGoodsDto;
import stylistcrm.core.services.order.enums.EnumInvoiceOrderType;

import java.util.List;

@Repository
public class PurchaseOrderGoodsDaoImpl implements PurchaseOrderGoodsDao {

	@Autowired
	private PurchaseOrderGoodsMapper purchaseOrderGoodsMapper;
	
	@Override
	public int deleteByPrimaryKey(Integer id) {
		return purchaseOrderGoodsMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int insert(PurchaseOrderGoods record) {
		return purchaseOrderGoodsMapper.insert(record);
	}

	@Override
	public int insertSelective(PurchaseOrderGoods record) {
		return purchaseOrderGoodsMapper.insertSelective(record);
	}

	@Override
	public PurchaseOrderGoods selectByPrimaryKey(Integer id) {
		return purchaseOrderGoodsMapper.selectByPrimaryKey(id);
	}

	@Override
	public int updateByPrimaryKeySelective(PurchaseOrderGoods record) {
		return purchaseOrderGoodsMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(PurchaseOrderGoods record) {
		return purchaseOrderGoodsMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<OrderGoodsDto> getGoodsList(Long userId) {
		List<OrderGoodsDto> purchaseOrderGoodsList=purchaseOrderGoodsMapper.getGoodsList(userId);
		if(null!=purchaseOrderGoodsList&&!purchaseOrderGoodsList.isEmpty()){
			for(OrderGoodsDto orderGoodsDto:purchaseOrderGoodsList){
				orderGoodsDto.setOrderType(EnumInvoiceOrderType.PURCHASE_INVOICE_ORDER.getCode());
				if(orderGoodsDto.getCount()!=null&&orderGoodsDto.getDeliveredCount()!=null){
					orderGoodsDto.setOrderDeliveredCount(orderGoodsDto.getCount()-orderGoodsDto.getDeliveredCount());
				}else{
					orderGoodsDto.setOrderDeliveredCount(0);
				}
			}
		}
		return purchaseOrderGoodsList;
	}
}
